package com.holdtime.jtt809dataservice.server.processor;

import com.edata.common.BytesUtil;
import com.holdtime.jtt809dataservice.common.event.MessageReceiveEvent;
import com.holdtime.jtt809dataservice.common.process.ProtocolProcessor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import protocol.jtt809.JTT809Packet;
import protocol.jtt809.JTT809Util;
import protocol.jtt809.VehicleColor;
import protocol.jtt809.body.RawBody;
import protocol.jtt809.body.UpExgMsgDmsAlarm;
import protocol.jtt809.body.UpWarnMsg;
import protocol.jtt809.body.UpWarnMsgAdptInfo;

/**
 * author: sunwei
 * Date: 2022/12/15 16:22
 * Description:
 */
@Slf4j
@Component
public class UpWarnMsgProcessor implements ProtocolProcessor {

    @Override
    public int getMsgID() {
        return UpWarnMsg.MSG_ID;
    }

    @Override
    public void process(MessageReceiveEvent event) {
        JTT809Packet<RawBody> source = event.getSource();
        UpWarnMsg<RawBody> upWarnMsg = new UpWarnMsg<>();
        upWarnMsg.setData(new RawBody());
        upWarnMsg.from(source.getBody().getRaw());
        if (upWarnMsg.getDataType() == UpExgMsgDmsAlarm.MSG_ID) {
            log.warn("收到告警上报数据:{}", BytesUtil.changebytetohexstring(JTT809Util.antisense(upWarnMsg.array())));
            long dataLength = upWarnMsg.getDataLength();
            if (dataLength > 65) {
                log.warn("收到DSM告警上报数据:{}", BytesUtil.changebytetohexstring(JTT809Util.antisense(upWarnMsg.array())));
                upExgMsgDmsAlarm(upWarnMsg);
            } else {
                upExgWarnMsgAdptInfo(upWarnMsg);
            }
        }

    }

    private void upExgMsgDmsAlarm(UpWarnMsg<RawBody> upWarnMsg) {
        UpExgMsgDmsAlarm upExgMsgDmsAlarm = new UpExgMsgDmsAlarm();
        upExgMsgDmsAlarm.from(upWarnMsg.getData().array());
        byte warnSrc = upExgMsgDmsAlarm.getWarnSrc();
        if ((short) warnSrc == 0x01) {
            //终端报警
            int warnType = upExgMsgDmsAlarm.getWarnType();
            switch (warnType) {
                case 0x0120:
                case 0x000D:
                case 0x000E:
                case 0x000F:
                case 0x0010:
                    //超时驾驶报警
                    //凌晨 2 时至 5 时行车报警
                    this.saveDmsAlarmToTs(upExgMsgDmsAlarm, upWarnMsg.getVehicleNo(), upWarnMsg.getVehicleColor());
                    break;
            }
        }
        //this.saveDmsAlarm(upExgMsgDmsAlarm, upWarnMsg.getVehicleNo(), upWarnMsg.getVehicleColor());
    }

    public void upExgWarnMsgAdptInfo(UpWarnMsg<RawBody> upWarnMsg) {
        upExgWarnMsgAdptInfo(upWarnMsg);
        UpWarnMsgAdptInfo upWarnMsgAdptInfo = new UpWarnMsgAdptInfo();
        upWarnMsgAdptInfo.from(upWarnMsg.getData().array());
        byte warnSrc = upWarnMsgAdptInfo.getWarnSrc();
        if ((short) warnSrc == 0x01) {
            //终端报警
            int warnType = upWarnMsgAdptInfo.getWarnType();
            switch (warnType) {
                case 0x0120:
                case 0x000D:
                case 0x000E:
                case 0x000F:
                case 0x0010:
                    log.warn("收到DSM告警上报数据:{}", BytesUtil.changebytetohexstring(JTT809Util.antisense(upWarnMsg.array())));
            }
        }
    }

    public void saveDmsAlarm(UpExgMsgDmsAlarm upExgMsgDmsAlarm, String vehicleNo, VehicleColor vehicleColor) {

    }

    public void saveDmsAlarmToTs(UpExgMsgDmsAlarm upExgMsgDmsAlarm, String vehicleNo, VehicleColor vehicleColor) {

    }

}
